package com.itzyf.pokemondata.activity.illustration

import android.content.Intent
import android.text.TextUtils
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.DbSuperSkillDo
import com.itzyf.pokemondata.utils.OnRefreshListener
import com.itzyf.pokemondata.utils.setOnRefreshListener
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_list.mBtnSearch
import kotlinx.android.synthetic.main.activity_list.mSearchView
import kotlinx.android.synthetic.main.activity_list.pull_to_refresh
import kotlinx.android.synthetic.main.activity_list.rv_list
import kotlinx.android.synthetic.main.activity_list_filter.*
import org.litepal.LitePal

/**
 * @author 依风听雨
 * @version 创建时间：2019/04/29 19:36
 */
class SuperSkillActivity : BaseActivity() {
    var mDatas: MutableList<DbSuperSkillDo?> = ArrayList()
    private lateinit var mAdapter: QuickAdapter
    var index = 0
    private var queryProperty = ""
    private lateinit var propertyList: Array<String>
    private var queryType = ""
    private val typeList = arrayListOf("全部", "物理", "特殊", "变化")

    override fun handlerActivity() {
        propertyList = resources.getStringArray(R.array.propertyList)
        mAdapter = QuickAdapter(R.layout.item_text_skill, mDatas)
        rv_list.adapter = mAdapter

        rv_list.layoutManager = LinearLayoutManager(this)
        mAdapter.setOnLoadMoreListener({ queryDatas(index++) }, rv_list)

        pull_to_refresh.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh() {
                index = 0
                queryDatas(index)
            }
        })
        mSearchView.hint = "请输入超级招式名称"
        mBtnSearch.setOnClickListener {
            val query = mSearchView.text.toString()
            queryDatas(query = query)
        }
        mSearchView.setOnEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val query = mSearchView.text.toString()
                queryDatas(query = query)
                return@OnEditorActionListener true
            }
            false
        })
        mAdapter.setOnItemClickListener { _, _, position ->
            val intent = Intent(this@SuperSkillActivity, SkillDetailActivity::class.java)
            intent.putExtra("id", mDatas[position]!!.id)
            intent.putExtra("type", 1)
            startActivity(intent)
        }

        spProperty.setItems(propertyList.toList())
        spProperty.setOnItemSelectedListener { _, position, _, _ ->
            queryProperty = propertyList[position]
            index = 0
            queryDatas(index)
        }
        tvDiscount.text = "类型"
        spDiscount.setItems(typeList)
        spDiscount.setOnItemSelectedListener { _, position, _, _ ->
            queryType = typeList[position]
            index = 0
            queryDatas(index)
        }
        queryDatas()
    }

    /**
     * 查询数据
     */
    fun queryDatas(index: Int = 0, pageSize: Int = 20, query: String = "") {
        var where = ""
        if (!TextUtils.isEmpty(query)) {
            where += " and cname like '%$query%'"
        }
        if (!TextUtils.isEmpty(queryProperty)) {
            if (queryProperty == "全部") {
                queryProperty = ""
            }
            if (queryProperty == "地面") {
                queryProperty = "地上"
            }
            where += " and ctype like '%$queryProperty%'"
        }
        if (!TextUtils.isEmpty(queryType)) {
            if (queryType == "全部") {
                queryType = ""
            }
            where += " and property like '%$queryType%'"
        }

        if (where.startsWith(" and")) {
            where = where.substring(4)
        }

        val dataList =
            LitePal
                .limit(pageSize)
                .offset(index * pageSize)
                .order("createdAt desc")
                .where(where)
                .find(DbSuperSkillDo::class.java)

        if (index == 0) {
            mDatas.clear()
            pull_to_refresh.finishRefresh()
        }

        for (obj in dataList) {
            mDatas.add(obj)
        }

        if (dataList.size < pageSize) {
            mAdapter.loadMoreEnd()
        } else {
            mAdapter.loadMoreComplete()
        }
        mAdapter.notifyDataSetChanged()
    }

    internal inner class QuickAdapter(layoutResId: Int, data: List<DbSuperSkillDo?>) :
        BaseQuickAdapter<DbSuperSkillDo, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: DbSuperSkillDo?) {
            item?.let {
                helper.setText(R.id.text, item.cname)
                helper.setText(R.id.tvCType, "属性：${it.ctype}")
                helper.setText(R.id.tvTooltip, item.effect)
                helper.setText(R.id.tvAttr, "类型：${it.property}")
                helper.setText(R.id.tvPower, "威力：${it.power}")
                helper.setText(R.id.tvShoot, "命中：${it.hitRate}")
                helper.setText(R.id.tvDetail, item.detail)
                if (TextUtils.isEmpty(it.detail)) {
                    helper.setGone(R.id.tvDetail, false)
                    helper.setGone(R.id.llDetail, false)
                }else{
                    helper.setGone(R.id.tvDetail, true)
                    helper.setGone(R.id.llDetail, true)
                }
            }
        }
    }


    override fun getContentViewId() = R.layout.activity_list_filter

    override fun getToolbarTitle() = "超级招式图鉴"

}